﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kokomo.Mirror.Ia32
{
	public class Register : SimpleOperand
	{
		internal Register(
			int number,
			string name,
			OperandSize size,
			RegisterType type
			)
			: base(size)
		{
			this.Number = number;
			this.Name = name;
			this.Type = type;
		}

		public override string ToString()
		{
			return this.Name.ToLower();
		}

		public int Number { get; private set; }
		public string Name { get; private set; }
		public RegisterType Type { get; private set; }

		public static Register Get(
			int code,
			OperandSize size,
			RegisterType type
			)
		{
			Register register;
			if (type == RegisterType.GeneralPurpose)
			{
				var q =
					from reg in Registers._registers
					where
						(reg.Number == code)
						&& (reg.Size == size)
						&& (reg.Type == type)
					select reg
						;

				register = q.FirstOrDefault();
			}
			else
			{
				var q =
					from reg in Registers._registers
					where
						(reg.Number == code)
						&& (reg.Type == type)
					select reg
						;

				register = q.FirstOrDefault();
			}

			return register;
		}
	}
}
